package com.example.billin.opengltest.shader.program

import android.content.Context
import android.graphics.Color
import android.opengl.GLES20
import com.example.billin.opengltest.R
import java.nio.ByteBuffer
import java.nio.ByteOrder

abstract class GradientProgram(context: Context) : Program(context,
    R.raw.vertex_base_color_gradient,
    R.raw.fragment_base_color_gradient
) {

    private val uMatrix = GLES20.glGetUniformLocation(programId, "uMatrix")

    private val uSize = GLES20.glGetUniformLocation(programId, "uSize")

    protected val ap = GLES20.glGetAttribLocation(programId, "ap")

    protected val aColor = GLES20.glGetAttribLocation(programId, "aColor")

    protected val uColor = GLES20.glGetUniformLocation(programId, "uColor")

    fun setSize(size: Int) {
        GLES20.glUniform1f(uSize, size.toFloat())
    }

    fun setMatrix(matrix: FloatArray) {
        GLES20.glUniformMatrix4fv(uMatrix, 1, false, matrix, 0)
    }
}

class GradientTriangleProgram(context: Context) : GradientProgram(context) {



    private var triangleVertexSize = 0

    /**
     * 支持绘制 300 个三角形(这个内存开得有点大吧  没必要申请这么多)
     */
    private val floatBuffer = ByteBuffer.allocateDirect(300 * 3 * 6 * 4)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer()


    fun bindTriangles(triangles: FloatArray, colorV0: Int, colorV1: Int, colorV2: Int, length: Int) {
        floatBuffer.clear()
        floatBuffer.put(triangles)
        floatBuffer.position(0)

        for (i in 0 until length step 2) {
            val ci = i / 2
            floatBuffer.put(triangles[i])
            floatBuffer.put(triangles[i + 1])

            val color = when (ci % 3) {
                0 -> colorV0
                1 -> colorV1
                2 -> colorV2
                else -> throw RuntimeException("impossible exception")
            }

            floatBuffer.put(Color.red(color).toFloat() / 255f)
            floatBuffer.put( Color.green(color).toFloat() / 255f)
            floatBuffer.put( Color.blue(color).toFloat() / 255f)
            floatBuffer.put( Color.alpha(color).toFloat() / 255f)
        }

        floatBuffer.position(0)                                             //一次6个点数据 float四个字节
        GLES20.glVertexAttribPointer(ap, 2, GLES20.GL_FLOAT, false, 6 * 4, floatBuffer)
        GLES20.glEnableVertexAttribArray(ap)

        floatBuffer.position(2)//从第二个开始读取(跳过ap)
        GLES20.glVertexAttribPointer(aColor, 4, GLES20.GL_FLOAT, false, 6 * 4, floatBuffer)
        GLES20.glEnableVertexAttribArray(aColor)

        triangleVertexSize = length / 2
    }

    override fun draw() {
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, triangleVertexSize)
    }
}